package com.xuecheng.order.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.commons.constants.OrdersConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.vo.CoursePubIndexVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.order.domain.Orders;
import com.xuecheng.order.mapper.OrdersMapper;
import com.xuecheng.order.service.OrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.search.api.SearchClient;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-09-23
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private SearchClient searchClient;

    @Override
    public ResponseResult createOrder(long coursePubId) {
        //1.获取用户ID
        Long userId = AuthInfoHolder.getUserId();
        Orders order = this.getById(userId);
        //2.校验用户是否已经购买
        LambdaQueryWrapper<Orders> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(Orders ::getCoursePubId,coursePubId);
        qw1.eq(Orders ::getUserId,userId);
        qw1.eq(Orders ::getStatus, OrdersConstants.ORDER_PAID_STATUS);//6002-已支付；6001-初始状态未支付；
        long count = this.count(qw1);
        if (count > 0) {
            throw new BusinessException(ErrorCode.ORDERERROR);
        }
        //3.检验用户是否已经下单
        LambdaQueryWrapper<Orders> qw = new LambdaQueryWrapper<>();
        qw.eq(Orders::getUserId,userId);
        qw.eq(Orders::getCoursePubId,coursePubId);
        Orders one = this.getOne(qw);
        if (ObjectUtil.isNotEmpty(one)) {
            return ResponseResult.okResult(one.getOrderNo());
        }
        //4.封装订单
        Orders orders = new Orders();
        //设置订单号
        orders.setOrderNo(IdUtil.simpleUUID());

        //4-1.查询课程发布详情
        ResponseResult<CoursePubIndexVo> responseResult = searchClient.getPubCourseById(String.valueOf(coursePubId));
        if ((responseResult.getCode()!=0)) {
            throw new BusinessException(ErrorCode.NOTFOUND);
        }
        CoursePubIndexVo pubIndexVo = responseResult.getData();
        orders.setCoursePubId(pubIndexVo.getCourse_id()); // 课程发布id
        orders.setCoursePubName(pubIndexVo.getName()); // 课程名称
        orders.setCompanyId(pubIndexVo.getCompany_id()); // 机构id
        orders.setCompanyName(pubIndexVo.getCompanyName()); // 机构名称
        orders.setUserId(userId);// 购买人id
        orders.setPrice(pubIndexVo.getPrice().longValue()); // 订单价格 单位分
        orders.setStatus(OrdersConstants.ORDER_INIT_STATUS); // 订单状态 未支付
        orders.setCreateDate(new Date()); // 下单时间

        //5.保存到数据库
        this.save(orders);
        String orderNo = orders.getOrderNo();
        //返回订单号
        return ResponseResult.okResult(orderNo);

    }
}
